package com.bocloud.cmp.web.controller.resource;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.web.controller.BasicController;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;
import com.bocloud.registry.utils.UrlTranslator;

@RestController
@RequestMapping("/vhost")
public class HostSystemController {

	private final String BASE_SERVICE = "/vhost";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private BasicController basicController;
	@Autowired
	private ServiceFactory serviceFactory;

	/**
	 * 创建
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = false) String params,
			HttpServletRequest request) {
		return basicController.create(params, BASE_SERVICE, request, HostSystemController.class.getSimpleName());
	}

	/**
	 * 列表
	 * 
	 * @param page
	 *            当前页码
	 * @param rows
	 *            页面数据大小
	 * @param params
	 *            查询参数
	 * @param sorter
	 *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple
	 *            简单查询标记，只有true和false,为false时返回资源池的详细信息，为true时只返回id和name值
	 * @param request
	 *            请求对象
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
		return basicController.list(page, rows, params, sorter, simple, BASE_SERVICE, request,
				HostSystemController.class.getSimpleName());
	}

	/**
	 * 更新
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = false) String params,
			HttpServletRequest request) {
		return basicController.modify(params, BASE_SERVICE, request, HostSystemController.class.getSimpleName());
	}

	/**
	 * 删除
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/remove", method = { RequestMethod.POST })
	public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.remove(params, BASE_SERVICE, request, HostSystemController.class.getSimpleName());
	}

	/**
	 * 详细
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	public BsmResult detail(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.detail(params, BASE_SERVICE, request, HostSystemController.class.getSimpleName());
	}

	/**
	 * 同步
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sync", method = { RequestMethod.POST })
	public BsmResult sync(@RequestParam(value = Common.PARAMS, required = false) String params,
			HttpServletRequest request) {
		JSONObject obj = JSONObject.parseObject(params);
		if (null != obj) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/sync", BoCloudMethod.OTHERS,
					null, paramMap, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	@RequestMapping(value = "/createVSwitch", method = { RequestMethod.POST })
	public BsmResult createVSwtich(@RequestParam(value = Common.PARAMS, required = false) String params,
			HttpServletRequest request) {
		JSONObject object = JSONObject.parseObject(params);
		if (null != object) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/createVSwitch",
					BoCloudMethod.OTHERS, null, paramMap, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 列表
	 * 
	 * @param page
	 *            当前页码
	 * @param rows
	 *            页面数据大小
	 * @param params
	 *            查询参数
	 * @param sorter
	 *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple
	 *            简单查询标记，只有true和false,为false时返回资源池的详细信息，为true时只返回id和name值
	 * @param request
	 *            请求对象
	 * @return
	 */
	@RequestMapping(value = "/listvmbyvhost", method = { RequestMethod.POST })
	public BsmResult listvmByvhost(@RequestParam(value = Common.PARAMS, required = false) String params,
			HttpServletRequest request) {
		if (null != params) {
			List<Param> paramList = JSONObject.parseArray(params, Param.class);
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, JSONObject.toJSONString(paramList.get(0)));
			RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/listvmbyvhost",
					BoCloudMethod.OTHERS, null, paramMap, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 获取监控数据
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/performance", method = { RequestMethod.GET })
	public BsmResult performance(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject obj = JSONObject.parseObject(params);
		if (null != obj) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/performance",
					BoCloudMethod.SUBOBJ, null, paramMap, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 断开连接
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/disconnect", method = { RequestMethod.POST })
	public BsmResult disconnection(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return operate(params, request, "disconnect");
	}

	/**
	 * 连接
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/reconnect", method = { RequestMethod.POST })
	public BsmResult reconnection(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return operate(params, request, "reconnect");
	}

	/**
	 * 进入维护模式
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/entermaintain", method = { RequestMethod.POST })
	public BsmResult maintain(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return operate(params, request, "entermaintain");
	}

	/**
	 * 退出维护模式
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/exitmaintain", method = { RequestMethod.POST })
	public BsmResult exitmaintain(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return operate(params, request, "exitmaintain");
	}

	/**
	 * 移动
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/move", method = { RequestMethod.POST })
	public BsmResult move(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject obj = JSONObject.parseObject(params);
		if (null != obj) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/move", BoCloudMethod.OPERATE,
					null, paramMap, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	private BsmResult operate(String params, HttpServletRequest request, String operate) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/" + operate, jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

}
